<?php
declare (strict_types=1);

namespace app\common\library\wechat;

use app\common\enum\OrderType as OrderTypeEnum;
use app\common\library\helper;
use app\common\model\OrderSub as OrderSubModel;
use app\common\model\UserOauth;
use app\store\model\Express as ExpressModel;


/**
 * 微信小程序用户管理类
 * Class WxUser
 * @package app\common\library\wechat
 */
class WxSendGood extends WxBase
{
    // 微信支付配置
    private $config;

    // 当前商城ID
    private $storeId;

    /**
     * 构造函数
     * @param array $config
     * @param int|null $storeId
     */
    public function __construct(array $config = [], ?int $storeId = null)
    {
        parent::__construct();
        $this->config = $config;
        $this->storeId = $storeId;
        if (!empty($this->config)) {
            $this->setConfig($this->config['app_id'], $this->config['app_secret']);
        }
    }
    //微信发货
    public function sendWechatOrder($order){
        $order_sub = OrderSubModel::getOrderSubBy($order['order_id']);
        if($order_sub['pay_money'] == '0.00'){//微信支付金额为0时，不去微信发货
            return false;
        }
        $token = $this->getAccessToken();
        if (!$token) {
            throwError('微信配置有误');
        }

        //获取用户openid
        if($order['user_id']){
            $userInfo = UserOauth::getOauth($order['user_id'],'MP-WEIXIN');
        }

        //获取快递公司
        if($order['express_id']){
            $model = new ExpressModel;
            $express = $model->detail($order['express_id']);
        }

        $transaction_id = $order['transaction_id']??'';
        $title = $order['title']??'';
        $openid = $userInfo['oauth_id']??'';
        $mobile = $order['mobile']??'';
        $tracking_no = $order['express_no']??'';
        $express_company = $express['kuaidi100_code']??'';
        $url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token={$token}";
        $upload_time = date('Y-m-d\TH:i:s.vP');
        $arrayData = [
            "order_key" => [
                "order_number_type" => 2,
                "transaction_id" => $transaction_id
            ],
            "delivery_mode" => 1,
            "logistics_type" => 1,
            "shipping_list" => [
                [
                    "item_desc" => $title,
                    "contact" => [
                        "receiver_contact" => substr_replace($mobile, '****', 3, 4)
                    ],
                    "tracking_no" => $tracking_no,
                    "express_company" => $express_company,
                ]
            ],
            "upload_time" => $upload_time,
            "payer" => [
                "openid" => $openid
            ]
        ];
        $result = helper::httpRequest($url, json_encode($arrayData,JSON_UNESCAPED_UNICODE));
        return json_decode($result, true);
    }
    //获取发货订单信息
    public function getWxSendOrderStatus($transaction_id)
    {
        $token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=" . $token;
        $data = [
            'transaction_id' => $transaction_id
        ];
        $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        $result = helper::httpRequest($url, $data);
        $result = json_decode($result, true);
        return $result;

    }
}